package org.stvd.controller.message;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.stvd.common.aspectj.lang.annotation.Log;
import org.stvd.common.aspectj.lang.enums.BusinessType;
import org.stvd.common.dto.QueryDTO;
import org.stvd.common.enums.MessageTypeEnum;
import org.stvd.common.security.SecurityUserHolder;
import org.stvd.common.utils.InterfaceResult;
import org.stvd.controller.BaseController;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.StringUtil;
import org.stvd.core.util.json.JsonUtils;
import org.stvd.entities.message.BsMessageInfo;
import org.stvd.service.message.BsMessageInfoService;

import net.sf.json.JSONObject;

/**
 * @Title: BsendMessageController
 * @Description: 业务发送消息控制类
 * @author houzx
 * @date 2020年10月10日
 */
@Controller
@RequestMapping("message/system")
public class BsSendMessageController extends BaseController{

    @Autowired
    private BsMessageInfoService bsMessageInfoService;
    
    private String tmpURL = "/message/send/";
    
    /**
     * @Description: 发送消息管理
     * @param bsMessageInfo
     * @return
     */
    @RequestMapping(value = "manage")
    public String onMessageManage(
        @ModelAttribute("bsMessageInfo") BsMessageInfo bsMessageInfo,
        @ModelAttribute("queryDTO") QueryDTO queryDTO){
        if(StringUtil.isEmpty(bsMessageInfo.getCreateUser())) {
            bsMessageInfo.setCreateUser(SecurityUserHolder.getCurrentUserId());
        }
        QueryResult<BsMessageInfo> queryResult = bsMessageInfoService.querySendMessageResult((pageNo - 1) * pageSize, pageSize,
            queryDTO.getQueryStartDate(), queryDTO.getQueryEndDate(), bsMessageInfo.getTitle(), bsMessageInfo.getCreateUser());
        map.put("queryResult", queryResult);
        map.put("messageTypeMap", MessageTypeEnum.messageTypeMap);
        return tmpURL + "BsSendMessageManage.html";
    }
    
    protected void loadParameterData(HttpServletRequest request, JSONObject jsonMap) {
        BsMessageInfo bsMessageInfo = new BsMessageInfo();
        QueryDTO queryDTO = new QueryDTO();
        map.put("bsMessageInfo", JsonUtils.textToBean(bsMessageInfo, String.valueOf(jsonMap)));
        map.put("queryDTO", JsonUtils.textToBean(queryDTO, String.valueOf(jsonMap)));
    }
    
    /**
     * @Description: 新增消息信息
     * @param bsMessageInfo
     * @return
     */
    @GetMapping(value = "add")
    public String onMessageAddLoad(@ModelAttribute(value="bsMessageInfo") BsMessageInfo bsMessageInfo){
        return tmpURL + "BsSendMessageAdd.html";
    }
    
    @Log(title = "新增消息通知", businessType =  BusinessType.INSERT)
    @PostMapping(value = "add")
    @ResponseBody
    public InterfaceResult<Map<String, Object>> onMessageAddAction(
        @Valid @ModelAttribute(value="bsMessageInfo") BsMessageInfo bsMessageInfo, 
        BindingResult bindingResult){
        InterfaceResult<Map<String, Object>> result = new InterfaceResult<Map<String,Object>>();
        if(bindingResult.hasErrors()) {
            result.setError(bindingResult.getAllErrors().get(0).getDefaultMessage());
            return result;
        }
        result.transferResult(bsMessageInfoService.insertMessageInfo(bsMessageInfo));
        return result;
    }
}
